package com.itheima.代码练习;

import com.itheima.System.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    private Student student;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("----欢迎来到学生管理系统----");
            System.out.println("1,添加学生");
            System.out.println("2，删除学生");
            System.out.println("3，修改学生");
            System.out.println("4，查看学生");
            System.out.println("5，退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent(sc, list);
                    break;
                case "2":
                    //removeStudent(list);
                    deleteStudent(sc, list);
                    break;
                case "3":

                    updateStudent(sc,list);
                    break;
                case "4":
                    queryStudent(sc, list);
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    return;
                default:
                    System.out.println("您输入的数据有误!");
                    break;
            }
        }
    }

    private static void updateStudent(Scanner sc,ArrayList<Student> list) {
        System.out.println("请输入要修改的学号：");
        String sid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getSid().equals(sid)){

            }
        }
    }

    private static void deleteStudent(Scanner sc, ArrayList<Student> list) {
        System.out.println("请输入要删除的学号：");
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index==-1){
            System.out.println("查无此人，请重新输入：");
        }
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (sid.equals(student.getSid())) {
                list.remove(i);
            }
        }
        System.out.println("删除成功");
    }

    private static void queryStudent(Scanner sc, ArrayList<Student> list) {
        System.out.println("学号\t姓名\t年龄\t\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getBirthday());
        }
    }

    private static void addStudent(Scanner sc, ArrayList<Student> list) {
        System.out.println("请输入学号:");
        String sid = sc.next();
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student student = new Student(sid, name, age, birthday);
        list.add(student);
        System.out.println("添加成功");
    }

    public static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                index = i;
            }
        }
        return index;
    }
}
